﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TileEngine.tiles;
using TileEngine.tiles.placable;

namespace TileEngine.items
{
    class TorchItem:Defaultitem
    {
        int frame = 0;
        int coolDown = 0;
        public TorchItem(Vector2 location)
        {
            this.location = location;
            velocity = new Vector2();
        }
        public override void mouseLeftClick(GameTime gametime)
        {
            if (coolDown < getCoolDown())
            {
                coolDown += gametime.ElapsedGameTime.Milliseconds;
                return;
            }
            Point mouseTile = new Point((int)((Mouse.GetState().X + Camera.Location.X) / Global.tileSize.X), (int)((Mouse.GetState().Y + Camera.Location.Y) / Global.tileSize.Y));
            if ((new Vector2(mouseTile.X * Global.tileSize.X, mouseTile.Y * Global.tileSize.Y) - Global.currentWorld.player.Location).Length() < 100)
            {
                if (canPlace(mouseTile))
                {
                    if (Global.currentWorld.player.inventory.getItems(this.GetType(), 1))
                    {
                        Itile newTile = Activator.CreateInstance(getTile()) as Itile;
                        newTile.setCoord(mouseTile.X, mouseTile.Y);
                        Global.currentWorld.tiles[mouseTile.X, mouseTile.Y].tile = newTile;
                        coolDown = 0;
                    }
                }

            }
        }
        public override int getStackMax()
        {
            return 100;
        }
        int getCoolDown()//miliseconds
        {
            return 200;
        }
        public override Type getTile()
        {
            return typeof(Torch);
        }
        bool canPlace(Point p)
        {
            if (Global.currentWorld.tiles[p.X, p.Y].tile.GetType() != typeof(Empty))
                return false;
            if (Global.currentWorld.tiles[p.X + 1, p.Y].tile.GetType() != typeof(Empty))
                return true;
            if (Global.currentWorld.tiles[p.X - 1, p.Y].tile.GetType() != typeof(Empty))
                return true;
            if (Global.currentWorld.tiles[p.X, p.Y + 1].tile.GetType() != typeof(Empty))
                return true;
            if (Global.currentWorld.tiles[p.X, p.Y - 1].tile.GetType() != typeof(Empty))
                return true;
            return false;
        }
        public override Microsoft.Xna.Framework.Color getLight()
        {
            return new Color(255, 255, 255);
        }
        public override void useDraw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Color tint)
        {
            frame=(frame+1)%40;
            spriteBatch.Draw(Textures.torch,
                new Rectangle((int)Camera.screenDimension.X / 2+ (Global.currentWorld.player.isLookRight?1:-1)*
                    
                    4*Global.tileSize.X/5, 
                    
                    (int)Camera.screenDimension.Y / 2, 20, 20)
                , new Rectangle((int)(frame/10)*20,0,20,20), tint, (Global.currentWorld.player.isLookRight ? 1 : -1) *
                0,
                new Vector2(10,20), Global.currentWorld.player.isLookRight ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
            spriteBatch.Draw(Textures.player, new Rectangle((int)Camera.screenDimension.X / 2, (int)Camera.screenDimension.Y / 2 - 3 * Global.tileSize.Y / 8, 7, 18), new Rectangle(393, 0, 7, 21), tint, (Global.currentWorld.player.isLookRight ? 1 : -1) *
                MathHelper.PiOver2, new Vector2(3.5f, Global.tileSize.X), SpriteEffects.None, 0);
        }
        public override Texture2D getTexture()
        {
            return Textures.torchItem;
        }
    }
}
